package com.cgtong.android.mvp.presenter;

import android.text.TextUtils;

import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.http.callback.BaseCallBack;
import com.cgtong.android.http.callback.FollowCallBack;
import com.cgtong.android.http.callback.ProblemPageCallBack;
import com.cgtong.android.mvp.contract.ProblemPageContract;
import com.cgtong.android.mvp.model.ProblemPageModel;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.ApiResult;

import java.util.List;

/**
 * Created by chunc on 2017/10/23.
 */

public class ProblemPagePresenter implements ProblemPageContract.Presenter {

    private ProblemPageContract.Model mModle;
    private ProblemPageContract.View mView;

    public ProblemPagePresenter(ProblemPageContract.View view) {
        this.mView = view;
        mModle = new ProblemPageModel();
    }


    @Override
    public void httpProblemsData(String maxTime, final String searchTxt, int isDraft, int status, int isExpired, String latitude, String longitude,String beginTime,String endTime, final boolean isRefresh) {
        if (isRefresh) {
            mView.setEnableLoadMore(false);
            mView.setRefreshing(true);
        }
        mModle.httpProblemsData(maxTime, searchTxt, isDraft, status, isExpired, latitude, longitude,beginTime,endTime, new ProblemPageCallBack<List<ProblemBean>>() {
            @Override
            public void onHttpProblemsSuccess(List<ProblemBean> problemBeen) {
                if (mView != null) {
                    if (isRefresh) {
                        mView.setRefreshing(false);
                        mView.setEnableLoadMore(true);
                        if (problemBeen == null || problemBeen.size() == 0) {
                            //判断是否是搜索
                            if (!TextUtils.isEmpty(searchTxt)) {
                                mView.onSearchNullData();
                                return;
                            }
                            mView.onNullData();//非搜索空数据显示
                            return;
                        } else if (problemBeen.size() < ConstantManager.PROBLEMS_PAGE_SIZE) {
                            mView.onLoadMoreEnd(isRefresh);
                        }
                        mView.onHttpProblemsDataSuccess(problemBeen, isRefresh);
                    } else {
                        mView.onHttpProblemsDataSuccess(problemBeen, isRefresh);
                        if (problemBeen.size() < ConstantManager.PROBLEMS_PAGE_SIZE) {
                            mView.onLoadMoreEnd(isRefresh);
                        }
                    }
                }
            }


            @Override
            public void onError(ApiException e) {
                if (mView != null) {
                    mView.setRefreshing(false);
                    mView.onError(e);
                }
            }
        });
    }

    @Override
    public void httpMyDrafts() {
        if (mView != null) {
            mView.setEnableLoadMore(false);
            mView.setRefreshing(true);
        }
        mModle.httpMyDrafts(new ProblemPageCallBack<List<ProblemBean>>() {
            @Override
            public void onHttpProblemsSuccess(List<ProblemBean> problemBeen) {
                if (mView != null) {
                    mView.setEnableLoadMore(true);
                    mView.setRefreshing(false);
                    mView.onHttpDraftsSuccess(problemBeen);
                }
            }


            @Override
            public void onError(ApiException e) {
                if (mView != null) {
                    mView.setRefreshing(false);
                    mView.onError(e);
                }
            }
        });


    }

    @Override
    public void follow(final long id, final int position) {
        mModle.follow(id, new FollowCallBack<ApiResult>() {
            @Override
            public void onFollowUserSuccess(long id, ApiResult apiResult) {
                if (mView != null){
                    mView.onFollowSuccess(id, position, apiResult);
                }
            }

            @Override
            public void onError(ApiException e) {
                if (mView != null){
                    mView.onError(e);
                }
            }

        });
    }

    @Override
    public void deleteDraft(long id, final int position) {

        mModle.deleteDraft(id, new BaseCallBack<ApiResult>() {
            @Override
            public void onSuccess(ApiResult apiResult) {
                if (mView != null) {
                    mView.onDeleteDraftSuccess(position, apiResult);
                }
            }

            @Override
            public void onError(ApiException e) {
                if (mView != null)
                    mView.onError(e);
            }

        });
    }

    @Override
    public void onDestroy() {
        if (mView != null)
            mView = null;
    }

}
